<?php
header("Content-Type: text/xml;charset=utf-8");
require_once( './includes/WebStart.php' );

//this is the normal header applied to any Google sitemap.xml file 
 $term = sphinx_escape($_GET['term']);
 if ($term=='') return;
 $lastPos = mb_strrpos($term, ' ');
 $startWord = '';

 if (!$lastPos)
 {
	$last = $term;
	$prefix = '';
	$newWord = false;
 }
 else
 {
	$last = mb_substr($term, $lastPos+1);
	$prefix = mb_substr($term, 0, $lastPos);
 	$newWord = (mb_strlen($last)<1);
 }
 if ($newWord) {
	$lastPos = mb_strrpos($prefix, ' ');
 	$termSql = mysql_real_escape_string($prefix).' *';
 	if (!$lastPos || $lastPos==0)
 	{
		$last = $prefix;
		$prefix = '';
 	}
 	/*else
	{
 		$termSql = mysql_real_escape_string($prefix).' *';
		//$last = mb_substr($prefix, $lastPos+1);
		//$prefix = mb_substr($prefix, 0, $lastPos); 

	}*/
 } else {
 	$termSql = mysql_real_escape_string($term).'*';
 }
 if ($lastPos) $prefix .= ' ';

 $last = mb_trim($last);
 $dbr = wfGetDB( DB_SLAVE );
 $sql = "SELECT t.Artist, t.Album, t.Title, s.int1 pop FROM ss_tags t inner join searches s on s.idtags = t.idtags WHERE query='mode=all;$termSql;limit=40;sort=extended:countTagsOk desc;groupby=attr:songKey;groupsort=int1 desc;select=idtags as idtags,sum(@weight+countTagsOk) as int1;maxquerytime=50'";
 $res = $dbr->query( $sql );

// $pattern = "/(\\n|[^\p{L}\p{N}])".preg_quote($last, '/')."([^\p{L}\p{N}]?[\p{L}\p{N}]+)([^\\n]*)\\n/ui"; //\p{L}\p{N}]/ui";


//$d.="<sql>$sql-$last-$prefix</sql>";
//$d.="<p>".$pattern."</p>";
 $matches = array();
 while ($row = $dbr->fetchRow( $res )) {
	$type = 0;
	$key = null;
	$usepos = -1;
	$prefix = $term;
	$prefixes = mb_string2array($prefix);
	$prefixc = count($prefixes);

	$album = mb_convert_case($row[Album], MB_CASE_LOWER);
	$title = mb_convert_case($row[Title], MB_CASE_LOWER);
	$artist = mb_convert_case($row[Artist], MB_CASE_LOWER);

	if ($album!=null) {
		$tmppos = mb_strripos($prefix, $album);
		$tmpend = $tmppos+mb_strlen($album);
		if ($tmppos!=false && ($tmppos==0 || $prefixes[$tmppos-1]==' ') && ($tmpend==$prefixc || $prefixes[$tmpend]==' ')) {
			$type |= 2;
			$usepos = $tmpend;
		}
	}

	$tmppos = mb_strripos($prefix, $title);
	$tmpend = $tmppos+mb_strlen($title);
	if ($tmppos!==false && ($tmppos==0 || $prefixes[$tmppos-1]==' ') && ($tmpend==$prefixc || $prefixes[$tmpend]==' ')) {
		$type |= 4;
		if ($usepos<$tmpend) $usepos = $tmpend;
	}

	$tmppos = mb_strripos($prefix, $artist);
	$tmpend = $tmppos+mb_strlen($artist);
	if ($tmppos!==false && ($tmppos==0 || $prefixes[$tmppos-1]==' ') && ($tmpend==$prefixc || $prefixes[$tmpend]==' ')) {
		$type |= 1;
		if ($usepos<$tmpend) $usepos = $tmpend;
	}

	if ($usepos==-1)
	{
		$pattern = $term;
		$prefix = '';
	}
	else
	{
		$pattern = mb_trim(mb_substr($prefix, $usepos));
		$prefix = mb_substr($prefix, 0, $usepos).' ';
	}
//$d.="<type>$type</type>";	
		
	if (($type&1)==0) {
		$len = min(strlen($artist), strlen($pattern));
		if ($len>0 && strncmp($artist, $pattern, $len)==0) { $key = $artist; $type = max($type, 1); }
	}

	if (!$key && ($type&4)==0) {
		$len = min(strlen($title), strlen($pattern));
		if ($len>0 && $pattern=='' || strncmp($title, $pattern, $len)==0) { $key = $title; $type |= 4; }
	}
	 
	if (!$key && ($type&2)==0 && $album!=null) {
		$len = min(strlen($album), strlen($pattern));
		if ($len>0 && $pattern=='' || strncmp($album, $pattern, $len)==0) { $key = $album; $type |= 2; }
	}
	
//$d.="<pr>".htmlspecialchars("$type-$prefixc-$usepos-....-$prefix-$pattern-($artist-$title-$album)-$len => $key", ENT_QUOTES)."</pr>";	
	if (!$key) continue;

	switch ($type)
	{
		case 1:
			$matches["$prefix$key"] = wfMsg("artist");
			break;
		case 2:
			$matches["$prefix$key"] = wfMsg("release_from")." ".$artist;
			break;
		case 4:
		case 6:
			$matches["$prefix$key"] = wfMsg("song_from")." ".$artist;
			break;
		case 3:
			$matches["$prefix$key"] = wfMsg("release");
			break;
		case 5:
			if ($album==null)
				$matches["$prefix$key"] = wfMsg("song");
			else
				$matches["$prefix$key"] = wfMsg("song_from_album")." ".$album;
			break;
		case 7:
			$matches["$prefix$key"] = wfMsg("song");
			break;
	}


	if (count($matches)>5) break;
 }//end of while
         
 $count = 0;
 $out = "<results>";
 foreach ($matches as $key => $value)
 { 
 	if ($key!=$term) {
		$out .= "<rs id='$count' info='".htmlspecialchars($value, ENT_QUOTES)."'>".htmlspecialchars($key)."</rs>";
		$count++;
	}
 }
 $out .= '</results>';
 echo $out;
    
 $dbr->freeResult ( $res );
